/**
 * Created by matengfei on 15/8/22.
 */


///* OK
$(document).ready(function () {
    //alert("文档初始化完毕");

    //bind_event();
    control_input();

});
//*/


function control_input() { //availableTags
    $(function () {

        // 使能根据用户的输入，自动显示且可以填充
        $("#input_word").autocomplete({
            //source: availableTags
            source: function (request, response) {

                var word = request.term;  //得到用户输入
                $.ajax({
                    type: "GET",
                    url: "search.php", //
                    data: "word=" + word,
                    success: function (msg) {
                        //var result = eval(msg);
                        var result = msg.split(',');
                        //control_input(result);
                        //alert( "Success: " + result);
                        console.log(result);
                        response(result);   //

                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        // 通常 textStatus 和 errorThrown 之中
                        // 只有一个会包含信息
                    }
                });


            }

        });
    });

}

function bind_event() {
    $("#input_word").bind('input', function () {  // propertychange

        var word = $("#input_word").val();
        //alert("");
        console.log(word);

        $.ajax({
            type: "GET",
            url: "search.php", //
            data: "word=" + word,
            success: function (msg) {
                //var result = eval(msg);
                var result = msg.split(',');
                //control_input(result);
                //alert( "Success: " + result);
                console.log(result);


            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                // 通常 textStatus 和 errorThrown 之中
                // 只有一个会包含信息
            }
        });


    });
}


///*
function add_word() {

    // ajax请求，填入单词
    var input_word = document.getElementById("input_word").value;
    //alert(""+input_word.value);

    // 发送翻译请求
    $.ajax({
        type: "GET",
        url: "index.php", // ci/api.php/fuc
        data: "word=" + input_word,
        success: function (msg) {
            var obj = JSON.parse(msg);
            var trans_result = obj["trans_result"];
            $("#output").text(trans_result[0]["dst"]);
            $("#output_translate").text(msg);
            //alert("Success: " + msg);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            // 通常 textStatus 和 errorThrown 之中
            // 只有一个会包含信息
            alert("error: " + errorThrown);
        }
    });

    //
    var speech = "http://tts.baidu.com/text2audio?lan=zh&pid=101&ie=UTF-8&text=" + input_word + "&spd=2";  //spd控制速度
    //$("#speech").src(speech);

    var audio_control = document.getElementById("audio_control");
    /*
     var source= document.createElement("source");
     source.src = speech;
     source.type = "audio/mp3";
     audio_control.appendChild(source);
     //audio_control.play();
     */
    audio_control.src = speech;
    audio_control.autoplay = "autoplay";


    /*
     $.get("ci/api.php/fuc/index", function(data,status){
     alert("Data: " + data + "\nStatus: " + status);
     }).error(function( message ){

     });
     */

}
//*/